/*
 * SPDX-FileCopyrightText: 2010-2022 Espressif Systems (Shanghai) CO LTD
 *
 * SPDX-License-Identifier: CC0-1.0
 */

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "sdkconfig.h"

#include "lvgl.h"
#include <inttypes.h>
#include <stdio.h>

#include "esp_err.h"
#include "esp_log.h"
#include "esp_timer.h"

#include "src/widgets/label/lv_label.h"

#define TAG "LVGL"

static void btn_event_cb(lv_event_t *e) {
  lv_event_code_t code = lv_event_get_code(e);
  lv_obj_t *btn = lv_event_get_target(e);
  if (code == LV_EVENT_CLICKED) {
    uint32_t current_time = esp_timer_get_time() / 1000;
    static bool status = true;
    static uint32_t last_check_time = 0;
    if (current_time - last_check_time >= 300) {
      static uint8_t cnt = 0;
      cnt++;
      last_check_time = current_time;
      /*Get the first child of the button which is the label and change its
       * text*/
      lv_obj_t *label = lv_obj_get_child(btn, 0);

      if (status) {
        lv_obj_set_style_bg_color(btn, lv_palette_main(LV_PALETTE_CYAN), 0);
        lv_label_set_text(label, "LED Off");
      } else {
        lv_obj_set_style_bg_color(btn, lv_palette_main(LV_PALETTE_AMBER), 0);
        lv_label_set_text(label, "LED On");
      }
      status = !status;
    }
  }
}

/**
 * Create a button with a label and react on click event.
 */
void lv_example_get_started_2(void) {
  lv_obj_t *btn =
      lv_button_create(lv_screen_active()); /*Add a button the current screen*/

  lv_obj_set_style_bg_color(btn, lv_palette_main(LV_PALETTE_AMBER), 0);

  lv_obj_set_pos(btn, 80, 140);  /*Set its position*/
  lv_obj_set_size(btn, 120, 50); /*Set its size*/
  lv_obj_add_event_cb(btn, btn_event_cb, LV_EVENT_ALL,
                      NULL); /*Assign a callback to the button*/

  lv_obj_t *label = lv_label_create(btn); /*Add a label to the button*/
  lv_label_set_text(label, "Button");     /*Set the labels text*/
  lv_obj_center(label);

  lv_obj_t *btn1 =
      lv_button_create(lv_screen_active()); /*Add a button the current screen*/

  lv_obj_set_style_bg_color(btn1, lv_palette_main(LV_PALETTE_DEEP_ORANGE), 0);

  lv_obj_set_pos(btn1, 80, 60);   /*Set its position*/
  lv_obj_set_size(btn1, 120, 50); /*Set its size*/
  lv_obj_add_event_cb(btn1, NULL, LV_EVENT_ALL,
                      NULL); /*Assign a callback to the button*/

  lv_obj_t *label1 = lv_label_create(btn1); /*Add a label to the button*/
  lv_label_set_text(label1, "LED");      /*Set the labels text*/
  lv_obj_center(label1);
}

static void example_increase_lvgl_tick(void *arg) {
  /* Tell LVGL how many milliseconds has elapsed */
  lv_tick_inc(2);
}

extern void lv_port_disp_init(void);
extern void lv_port_indev_init(void);
void app_main(void) {
  lv_init();
  lv_port_disp_init();
  lv_port_indev_init();

  ESP_LOGI(TAG, "Install LVGL tick timer");
  // Tick interface for LVGL (using esp_timer to generate 2ms periodic event)
  const esp_timer_create_args_t lvgl_tick_timer_args = {
      .callback = &example_increase_lvgl_tick, .name = "lvgl_tick"};
  esp_timer_handle_t lvgl_tick_timer = NULL;
  ESP_ERROR_CHECK(esp_timer_create(&lvgl_tick_timer_args, &lvgl_tick_timer));
  ESP_ERROR_CHECK(esp_timer_start_periodic(lvgl_tick_timer, 2 * 1000));
  lv_example_get_started_2();

  /* handle the tasks of LVGL */
  while (1) {
    lv_task_handler();
    vTaskDelay(pdMS_TO_TICKS(30));
  }
}
